import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { RequestMemberLogin } from '../api/user.js'
import { useUserStore } from '../stores/user'
/**
 * 搜索业务
 */
//  export const useSearch = () => {
//  	const searchValue = ref('')
//  	return { searchValue }
 
//  	/**
//  	 * 加载更多
//  	 */
// 	const onLoad = () => {
//  		// 第一次加载和组件滚动到底部时，会触发 load 事件并将 loading 设置成 true
// 		// 注：当数据不在一屏时，会一直触发onLoad
//  		getProductList(currentNo++, 20)
//  	}

// 	/**
// 	 * 下拉刷新
// 	 */
// 	const onRefresh = () => {
// 		currentNo = 1
// 		productList.value = []
// 		finished.value = false
// 		getProductList(currentNo, 20)
// 	}

// 	return {
// 		onRefresh,
// 		onLoad,
// 		productList,
// 		loading,
// 		finished,
// 	}
// }


/**
 * 登录业务
 */
export const useMemberLogin = () => {
	const store = useUserStore()
	const router = useRouter()
	const username = ref('')
	const password = ref('')
	const onSubmit = async () => {
		const data = await RequestMemberLogin(username.value, password.value)
		
		const { resultCode, resultInfo, token } = data
		if (resultCode == 1) {
			// 1.保存用户信息
			store.saveUser({ nick: resultInfo.nick, headerimg: resultInfo.headerimg })
			// 2.保存token
			localStorage.setItem('TOKEN', token)
			localStorage.setItem('user', JSON.stringify(resultInfo))
			// 3.跳转到首页
			router.replace({path:'/home'})
		} else {
			Toast.fail('用户名或密码出错!')
		}
	}
	const onClickLeft = () => {
		router.push({path:'/'})

	}
	const register=()=>{
		router.push({path:'/register'})
		
		}
	return {
		onSubmit,
		onClickLeft,
		register,
		username,
		password,
	}
}

